package com.ruoyi.word.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.baidu.TransWebApi;
import com.ruoyi.common.utils.youdao.FanyiV3Util;
import com.ruoyi.common.domain.word.CommonWord;
import com.ruoyi.common.domain.word.CommonWordBaidu;
import com.ruoyi.common.domain.word.CommonWordTp;
import com.ruoyi.common.domain.word.CommonWordYoudao;
import com.ruoyi.word.dao.mapper.CommonWordBaiduMapper;
import com.ruoyi.word.dao.mapper.CommonWordMapper;
import com.ruoyi.word.dao.mapper.CommonWordTpMapper;
import com.ruoyi.word.dao.mapper.CommonWordYoudaoMapper;
import com.ruoyi.word.service.ICommonWordTpService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class CommonWordTpServiceImpl implements ICommonWordTpService {

    @Autowired
    private CommonWordTpMapper dao;

    @Autowired
    private CommonWordMapper wordDao;

    @Autowired
    private CommonWordBaiduMapper wordBaiduDao;
    @Autowired
    private CommonWordYoudaoMapper wordYoudaoDao;

    @Override
    public List<CommonWordTp> findByWhereStr(String whereStr, String orderField, String order) {
        return dao.findByWhereStr(whereStr, orderField, order);
    }

    @Override
    public int saveOrUpdate(CommonWordTp word) {
        return dao.insertSelective(word);
    }

    @Override
    public int dealFromYoudao() throws IOException {
        int pn = 1, ps = 100, total = 0;
        while (true) {
            PageHelper.startPage(pn++, ps);
            List<CommonWord> wordList = wordDao.findByWhereStr(null, "id", "asc");
            total += wordList.size();
            for (CommonWord w:wordList) {
                if(wordYoudaoDao.existsWithPrimaryKey(w.getId())) {
                    continue;
                }
                JSONObject data = FanyiV3Util.transform(w.getName());
                if(data == null) {
                    continue;
                }
                CommonWordYoudao word = getWordFromYoudao(data, w);
                if(word != null) {
                    wordYoudaoDao.insertSelective(word);
                }
            }
            if(wordList.size() < ps) {
                return total;
            }
        }
    }

    private static CommonWordYoudao getWordFromYoudao(JSONObject json, CommonWord w){
        CommonWordYoudao word = new CommonWordYoudao();
        word.setId(w.getId());
        word.setName(w.getName());
        word.setData(JSON.toJSONString(json));
        word.setAddTime(new Date());

        // 简单释义
        JSONArray translation = json.getJSONArray("translation");
        if(CollectionUtils.isNotEmpty(translation)) {
            word.setSimplePh(StringUtils.join(translation, "; "));
        }
        // 复杂释义
        JSONObject basic = json.getJSONObject("basic");
        if(basic != null) {
            JSONArray explains = basic.getJSONArray("explains");
            if(CollectionUtils.isNotEmpty(explains)) {
                word.setCompletePh(StringUtils.join(explains, "\r\n"));
            }
        }
        return word;
    }


    @Override
    public int dealFromBaidu() throws Exception {
        int ps = 100, total = 0;
        while (true) {
            // 随机取1000条
            int pn = (int)(Math.random() * 5500 + 1);
            PageHelper.startPage(pn++, ps);
            List<CommonWord> wordList = wordDao.findByWhereStr(null, "id", "asc");
            total += wordList.size();
            for(CommonWord w: wordList) {
                if (wordBaiduDao.existsWithPrimaryKey(w.getId())) {
                    continue;
                }
                JSONObject data = TransWebApi.trans(w.getName());
                CommonWordBaidu word = getWordFromBaidu(data, w);
                if (word != null) {
                    wordBaiduDao.insertSelective(word);
                }
            }
        }
    }

    private static CommonWordBaidu getWordFromBaidu(JSONObject json, CommonWord w){
        CommonWordBaidu word = new CommonWordBaidu();
        word.setId(w.getId());
        word.setName(w.getName());
        word.setData(JSON.toJSONString(json));
        word.setAddTime(new Date());

        JSONObject dict_result = json.getJSONObject("dict_result");
        if(dict_result != null) {
            JSONObject simple_means = dict_result.getJSONObject("simple_means");
            if (simple_means != null) {
                // 获取简明释义
                JSONArray word_means = simple_means.getJSONArray("word_means");
                if (CollectionUtils.isNotEmpty(word_means)) {
                    word.setSimplePh(StringUtils.join(word_means, "\r\n"));
                }
                // 获取完整释义
                JSONArray symbols = simple_means.getJSONArray("symbols");
                if (CollectionUtils.isNotEmpty(symbols)) {
                    JSONArray parts = ((JSONObject) symbols.get(0)).getJSONArray("parts");
                    if (CollectionUtils.isNotEmpty(parts)) {
                        String cp = "";
                        for (Object p : parts) {
                            JSONObject pp = (JSONObject) p;
                            String part = pp.getString("part");
                            JSONArray means = pp.getJSONArray("means");
                            if (CollectionUtils.isNotEmpty(means)) {
                                cp += ((StringUtils.isNotBlank(part) ? (part + " "): "") +
                                        StringUtils.join(means, "; ") +
                                        "\r\n");
                            }
                        }
                        word.setCompletePh(cp);
                    }
                }
            }
        }
        return word;
    }

    public static void main(String[] args) throws InterruptedException, IOException {
//        JSONObject json = TransWebApi.trans("could");
//        CommonWordBaidu word = getWordFromBaidu(json, new CommonWord("could"));
        JSONObject json = FanyiV3Util.transform("could");
        if(json == null) {
            return;
        }
        CommonWordYoudao word = getWordFromYoudao(json, new CommonWord("could"));
        word.setData(null);
        System.out.println(word.getCompletePh());
        System.out.println(word.getSimplePh());
    }
}
